const { userModel, reaNameModel } = require("../model");
//添加实名认证
exports.addReaName = async (req,res)=>{
    const { reaName , cardType , cardNumber , cardImg } = req.body;
    let user = req.user
    // 检查是否已经添加过
    let isAdded = await userModel.findOne({_id:user._id})
   
    if(isAdded.realName==1||isAdded.realName==2) {
        console.log(111);
        return res.status(400).json({
            code:400,
            msg:'您已经添加过实名认证信息',
            data:null
        })
    }
    let result = await reaNameModel.findOne({user:user._id})
    if(result&&(result.status==1||result.status==2)) {
        return res.status(400).json({
            code:400,
            msg:'您已经添加过实名认证信息',
            data:null
            })
    }
    if(!reaName||!cardType||!cardNumber||!cardImg) {
        return res.status(400).json({
            code:400,
            msg:'请填写完整信息',
            data:null
        })
    }
   try {
     //添加实名信息
     let dbBack = new reaNameModel ({
        reaName,
        cardType,
        cardNumber,
        cardImg,
        user:user._id
    })
    await dbBack.save()
    // 修改用户表
    await userModel.findByIdAndUpdate({_id:user._id},{realName:1})
    res.status(200).json({
        code:200,
        message:'添加成功',
        data:null
    })
   } catch (error) {
    res.status(500).json({
        code:500,
        message:'添加失败',
        data:null
    })
   }

}
//获取实名认证列表
exports.getReaNameList = async (req,res)=>{
    const { pageNo=1,pageSize=10 } = req.query
    try {
        let result = await reaNameModel.find().skip((pageNo-1)*pageSize).limit(pageSize).populate('user').sort({createAt:-1})
        let total = await (await reaNameModel.find()).length
        res.status(200).json({
            code:200,
            message:'获取成功',
            data:result,
            total
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'获取失败',
            data:null
        })
    }
}
//获取实名认证详情
exports.getReaNameDetail = async (req,res)=>{
    const { id } = req.query
    if(!id) {
        return res.status(400).json({
            code:400,
            message:'参数错误',
            data:null
        })
    }
    try {
        let result = await reaNameModel.findById(id).populate('user')
        res.status(200).json({
            code:200,
            message:'获取成功',
            data:result
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'服务器出现问题，获取失败',
            data:null
        })
    }

}
//通过实名认证
exports.passReaName = async (req,res)=>{
    const { id } = req.query
    if(!id) {
        return res.status(400).json({
          code:400,
          message:'参数不正确',
          data:null  
        })
    }
    try {
        let result = await reaNameModel.findByIdAndUpdate({_id:id},{status:2})
        await userModel.findByIdAndUpdate({_id:result.user?._id},{realName:2})
        res.status(200).json({
            code:200,
            message:'审核通过',
            data:null
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'服务器出现问题，审核失败',
            data:null
        })
    }
}
//没通过实名的回调
exports.nopassReaName = async (req,res)=>{
    const { id } = req.query
    if(!id) {
        return res.status(400).json({
          code:400,
          message:'参数不正确',
          data:null  
        })
    }
    try {
        let result = await reaNameModel.findByIdAndUpdate({_id:id},{status:3})
        await userModel.findByIdAndUpdate({_id:result.user?._id},{realName:3})
        res.status(200).json({
            code:200,
            message:'已拒绝该请求',
            data:null
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'服务器出现问题，审核失败',
            data:null
        })
    }
}
//删除实名认证的回调
exports.deleteReaName = async (req,res)=>{
    const { id } = req.query
    if(!id) {
        return res.status(400).json({
          code:400,
          message:'参数不正确',
          data:null
          
        })
    }
    try {
        let result = await reaNameModel.findByIdAndDelete({_id:id})
        await userModel.findByIdAndUpdate({_id:result.user?._id},{realName:0})
        res.status(200).json({
            code:200,
            message:'删除成功',
            data:null
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'服务器出现问题，删除失败',
            data:null
        })
    }
}
//获取实名信息
exports.getReaName = async (req,res)=>{
    const { userId } = req.query
    if(!userId) {
        return res.status(400).json({
            code:400,
            message:'参数错误',
            data:null
        })
    }
    try {
        let result = await reaNameModel.findOne({user:userId})
        res.status(200).json({
            code:200,
            message:'获取成功',
            data:result
        })
    }catch (error) {
       res.status(500).json({
            code:500,
            message:'服务器出现问题，获取失败',
            data:null
       })
    }
}